//
//  EditTankInfoViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/7/25.
//

import UIKit
import RxSwift
import RxCocoa

class EditTankInfoViewController: BaseViewController {
    var refreshDetailClosure: (()->Void)?
    private lazy var disposeBag = DisposeBag()
    var customView = EditTankInfoView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        //如果没有tank ，要默认加一行（因为tank 信息只有一行）
        var listDeepCopy = [InstallationDetailSystemDetailForHeaterModel]()
        if JobDetailModelHelper.shared.jobDetailModel.system_detail.heaters.count > 0 {
            listDeepCopy = JobDetailModelHelper.shared.jobDetailModel.system_detail.heaters.clone()
        }else{
            listDeepCopy.append(InstallationDetailSystemDetailForHeaterModel())
        }
        self.customView.setData(listDeepCopy)
    }
    func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.editTankInformation())
        //MARK: - save 事件
        self.customView.saveBtn.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.customView.endEditing(true)
            strongSelf.saveTankInfo()
        }).disposed(by: self.disposeBag)
        //MARK: - 增加一行Panel
        self.customView.addBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.customView.tableView.addOneTank()
            strongSelf.customView.tableView.layoutIfNeeded()//会强制重绘并等待完成。
            //table view加载完，跳转到最后新增加的item的位置
            strongSelf.customView.tableView.scrollToRow(at: IndexPath(row: strongSelf.customView.tableView.list.count - 1, section: 0), at: UITableView.ScrollPosition.none, animated: true)
            
        }.disposed(by: self.disposeBag)
        
    }
    private func saveTankInfo(){
        let vm = EditJobInfoService()
        vm.setBlockAfterNetwork { [weak self] (result, _) in
            guard let strongSelf = self else {return}
            ModelViewUtil.shared.hideUpdatingView()
            if let data = result as? PVDDetailModel{
                JobDetailModelHelper.shared.jobDetailModel.system_detail = data.system_detail
            }
            strongSelf.refreshDetailClosure?()//panel更新后，要刷新detail中底部按钮barcode计划数量
            strongSelf.navigationController?.popViewController(animated: true)
            
        } errorBlock: {  (code, error) in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        ModelViewUtil.shared.showLoadingView(R.string.localizable.updating())
        vm.updateSystemInfo(heaterList: self.customView.tableView.list + self.customView.tableView.delList, spvCheck: JobDetailModelHelper.shared.jobDetailModel.spv_check)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
}
